package com.zkingsoft.action;

import static com.zkingsoft.common.constance.AppConstance.SAFEPATH;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.WebUtil;
import com.matrix.core.web.BaseAction;
import com.zkingsoft.bean.BizMaterial;
import com.zkingsoft.service.BizMaterialService;

/**
 * 素材管理
 * 
 * @author JIANGYOUYAO
 * @email 935090232@qq.com
 * @date 2018年9月19日
 */
@Controller
@RequestMapping(value = "admin/material")
public class MaterialAction extends BaseAction {

	@Autowired
	private BizMaterialService materialService;
	@Value("${file_storage_path}")
	private String fileStoragePath;
	@Value("${static_resource_url}")
	private String nginxUrl;
	// 记录编辑前的值Before_Edit_Value
	public static final String BEV = "BizMaterial_BEV";

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(BizMaterial bizMaterial, PaginationVO pageVo) {
		return showList(materialService, bizMaterial, pageVo);
	}

	/**
	 * 新增
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addBizMaterial")
	public @ResponseBody AjaxResult addBizMaterial(BizMaterial bizMaterial) {
		if(bizMaterial.getImgPath()!=null){
			bizMaterial.setImg(bizMaterial.getImgPath().replace(nginxUrl, fileStoragePath));
		}
		int i = materialService.add(bizMaterial);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "素材");
		} else {
			throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
		}
	}

	/**
	 * 修改
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/modifyBizMaterial")
	public @ResponseBody AjaxResult modifyBizMaterial(BizMaterial bizMaterial) {
		bizMaterial.setId(((BizMaterial) WebUtil.getSessionAttribute(BEV)).getId());
		if(bizMaterial.getImgPath()!=null){
			bizMaterial.setImg(bizMaterial.getImgPath().replace(nginxUrl, fileStoragePath));
		}
		AjaxResult result = modify(materialService, WebUtil.getSessionAttribute(BEV), bizMaterial, "素材");
		WebUtil.removeSessionAttribute(BEV);
		return result;
	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(String id) {
		BizMaterial bizMaterial;
		if (id != null) {
			bizMaterial = materialService.findById(id);
			WebUtil.getRequest().setAttribute("obj", bizMaterial);
			WebUtil.setSessionAttribute(BEV, bizMaterial);
		}
		return "admin/biz/Material-form";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(materialService, keys);
	}

}